/*
 * Copyright (c) 2009 Jesse McLaughlin (nzjess@gmail.com)
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *         http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.travelfusion.xmlclient.impl;

import org.travelfusion.xmlclient.TfXClient;
import org.travelfusion.xmlclient.login.Login;

import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Module;

/**
 * This is a convenience base-class for implementing factories to create pre-configured {@link TfXClient} instances.
 * <p>
 * Typical concrete implementations will override the necessary constructors, and implement the
 * {@link #configureFactory()} method to register the command handlers that will be used by clients using this factory.
 * <p>
 * Optionally, the {@link #configureInjector()} method may also be overridden to further configure Guice from this
 * class.
 * 
 * @see #configureFactory()
 * @see #configureInjector()
 * @see #getClient()
 * 
 * @author Jesse McLaughlin (nzjess@gmail.com)
 */
public abstract class BaseClientFactory extends AbstractModule {

  private final Injector injector;

  private final Login login;

  /**
   * Create a client factory using a custom set of Guice modules.
   */
  public BaseClientFactory(Module... modules) {
    this(null, modules);
  }

  /**
   * Create a client factory using a custom set of Guice modules. This a convenience constructor that also binds the
   * given (@link Login} instance.
   */
  public BaseClientFactory(Login login, Module... modules) {
    this.login = login;
    this.injector = Guice.createInjector(toModuleArray(this, modules));
    configureFactory();
  }

  private static Module[] toModuleArray(Module module, Module... modules) {
    Module[] moduleArray = new Module[modules.length + 1];
    moduleArray[0] = module;
    System.arraycopy(modules, 0, moduleArray, 1, modules.length);
    return moduleArray;
  }

  @Override
  protected final void configure() {
    if (login != null) {
      bind(Login.class).toInstance(login);
    }
    configureInjector();
  }

  /**
   * By default, this method does nothing. Override to configure the Guice injector from this class.
   */
  protected void configureInjector() {}

  /**
   * This method will typically register the command handlers that clients of this factory will use.
   */
  protected abstract void configureFactory();

  /**
   * Get a {@link TfXClient} instance from this factory.
   */
  public TfXClient getClient() {
    return injector.getInstance(TfXClient.class);
  }

  /**
   * Get the Guice injector used to configure the library.
   */
  public Injector getInjector() {
    return injector;
  }
}
